package com.qyhl.webtv.module_news.news.union.town.detail;

import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.MeetingRoomBean;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.commonlib.entity.news.UnionBean;
import com.qyhl.webtv.commonlib.entity.news.UnionRecBean;
import com.qyhl.webtv.module_news.common.NewsUrl;

import java.util.List;

/**
 * Created by qyhl on 2017/12/1.
 */

public class TownDetailModel implements TownDetailContract.TownDetailModel {
    private TownDetailPresenter mPresenter;

    public TownDetailModel(TownDetailPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 轮播
     *
     * @param sections
     */
    @Override
    public void getTop(String sections) {

        EasyHttp.get(NewsUrl.GET_TOWN_TOP)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("sections", sections)
                .execute(new SimpleCallBack<List<NewsBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setTopError("error or no data!");
                    }

                    @Override
                    public void onSuccess(List<NewsBean> newsBeans) {
                        mPresenter.setTop(newsBeans);
                    }
                });

    }

    /**
     * 栏目列表
     *
     * @param sections
     */
    @Override
    public void getMiddle(String sections) {

        EasyHttp.get(NewsUrl.GET_TOWN_LIST)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("type", "1")
                .params("setions", sections)
                .execute(new SimpleCallBack<List<UnionBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError(2, "暂无任何内容！");
                        } else {
                            mPresenter.setError(1, "解析出错，加载失败！");
                        }
                    }

                    @Override
                    public void onSuccess(List<UnionBean> unionBeans) {
                        if (unionBeans != null && unionBeans.size() > 0) {
                            mPresenter.setMiddle(unionBeans);
                        } else {
                            mPresenter.setError(2, "暂无任何内容！");
                        }
                    }
                });

    }

    /**
     * 新闻列表
     *
     * @param sections
     */
    @Override
    public void getBottom(String sections) {

        EasyHttp.get(NewsUrl.GET_TOWN_RECOMMEND)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("catalogID", sections)
                .execute(new SimpleCallBack<List<UnionRecBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setBottomError(1, "error or no data!");
                    }

                    @Override
                    public void onSuccess(List<UnionRecBean> unionRecBeans) {
                        if (unionRecBeans != null && unionRecBeans.size() > 0) {
                            mPresenter.setBottom(unionRecBeans);
                        } else {
                            mPresenter.setBottomError(0, "error or no data!");
                        }
                    }
                });

    }

    /**
     * 是否有议事厅
     *
     * @param catalogId
     */
    @Override
    public void getMeetingRoom(String catalogId) {

        EasyHttp.get(NewsUrl.GET_MEETTING_ROOM)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("catalogId", catalogId)
                .execute(new SimpleCallBack<MeetingRoomBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getMeetingError();
                    }

                    @Override
                    public void onSuccess(MeetingRoomBean meetingRoomBean) {
                        mPresenter.getMeetingSuccess(meetingRoomBean);
                    }
                });
    }
}
